package com.xuyiwei.xblog.modular.api;


import com.xuyiwei.xblog.business.article.service.IArticleService;
import com.xuyiwei.xblog.business.system.model.Config;
import com.xuyiwei.xblog.business.system.service.IConfigService;
import com.xuyiwei.xblog.core.base.constant.enums.FileUploadType;
import com.xuyiwei.xblog.core.base.controller.BaseController;
import com.xuyiwei.xblog.core.base.response.Result;
import com.xuyiwei.xblog.core.common.annotion.Permission;
import com.xuyiwei.xblog.core.exception.BizExceptionEnum;
import com.xuyiwei.xblog.core.exception.GunsException;
import com.xuyiwei.xblog.core.support.StrKit;
import com.xuyiwei.xblog.core.util.FileUtil;
import com.xuyiwei.xblog.core.util.OssUtil;
import com.xuyiwei.xblog.core.util.ToolUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 阿里云oss工具类
 */
@RestController
@RequestMapping("/alioss")
public class AliossController extends BaseController {

    @Autowired
    private IConfigService configService;

    @Autowired
    private IArticleService articleService;

    /**
     * 上传文件到七牛云
     *
     * @param file
     * @return
     */
    @PostMapping("/upload2Alioss")
    public Result<String> upload2Alioss(@RequestParam("file") MultipartFile file,String fileType) {
        Result<String> result = Result.instance();
        try{
            FileUploadType fileUploadType = null;

            if(StrKit.isBlank(fileType)){
                fileUploadType = FileUploadType.SIMPLE;
            }else{
                fileUploadType = FileUploadType.valueOf(fileType.toUpperCase());
            }
            String filePath = FileUtil.uploadToAlioss(file, fileUploadType, false);
//            result.setData(OssUtil.getUrl(filePath));
            result.setData(filePath);
            //下面是传统上传方法
//            String pictureName = UUID.randomUUID().toString() + "." + ToolUtil.getFileSuffix(picture.getOriginalFilename());
//            try {
//                String fileSavePath = gunsProperties.getFileUploadPath();
//                picture.transferTo(new File(fileSavePath + pictureName));
//            } catch (Exception e) {
//                throw new GunsException(BizExceptionEnum.UPLOAD_ERROR);
//            }
//            result.setData(pictureName);
        }catch (Exception e){
            result.setError("500","图片上传失败");
        }
        return result;
    }

    @PostMapping("/upload2AliossForMd")
    public Object upload2AliossForMd(@RequestParam("file") MultipartFile file) {
        String filePath = FileUtil.uploadToAlioss(file, FileUploadType.SIMPLE, false);
        Map<String, Object> resultMap = new HashMap<>(3);
        resultMap.put("success", 1);
        resultMap.put("message", "上传成功");
        resultMap.put("filename", OssUtil.getUrl(filePath));
        return resultMap;
    }

    /**
     * 发布文章选择阿里云图片时获取素材库
     *
     * @return
     */
    @PostMapping("/materialAlioss")
    public Result<List<String>> materialAlioss() {
        Result<List<String>> result = Result.instance();
        try{
            List<String> list = articleService.listMaterial();;
            result.setData(list);
        }catch (Exception e){
            result.setError("500","获取图片库出错");
        }
        return result;
    }


    /**
     * 发送消息通知
     *
     * @return
     */
//    @RequiresPermissions("notice")
//    @PostMapping("/notice")
//    public ResponseVO notice(String msg) throws UnsupportedEncodingException {
//        WebSocketUtil.sendNotificationMsg(msg, websocketServer.getOnlineUsers());
//        return ResultUtil.success("消息发送成功", articleService.listMaterial());
//    }

}
